/**
 * <p>功能:基于Fine-upload的文件上传。 <p>
 * @extends Ext.form.ComboBox
 * @author wys
 */
Ext.define('Jason.view.example.form.FineUpload.MainView', {
    extend: 'Ext.panel.Panel',
    xtype: 'fine-uploader-main',
    requires: [
        'Wys.fineUploader.FineUploader',
        'Wys.fineUploader.FineUploaderSingle'
    ],
    margin: '5',
    initComponent: function () {
        var me = this;

        me.items = [
            {
                xtype: 'fineUploader',
                config: {
                    request: {
                        url: cfg.requestUrl + 'services/fileUpload/multipicupload' + cfg.suffix, // 上传接口
                        params: { name: '' } // 自定义request参数
                    },
                    deleteFileURL: cfg.requestUrl + 'services/fileUpload/deletepicupload' + cfg.suffix, // 删除接口
                    validation: {
                        itemLimit: 6,
                        sizeLimit: 512000, // 1kb = 1024 bytes
                        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'zip']
                    },
                    onValidate: function (manualUploader, obj, btn) {
                        console.log('onValidate//...', arguments);
                        manualUploader.setParams({ name: 'aaabbb' });
                        //return false;
                    },
                    onUpload: function (manualUploader, id, name) {
                        console.log('onUpload//...', arguments);

                    },
                    onBeforeUpload: function () {
                        //alert('暂停');
                        //return false;
                    },
                    onAllComplete: function () {
                        console.log('onAllComplete//...', arguments);
                    },

                    initialFiles: []/* 初始化图片 */
                }
            },
            {
                xtype: 'button',
                text: '获取附件列表',
                margin: '20 10',
                handler: function () {
                    var me = this;
                    var uploadFile = me.up().down('fineUploader');
                    console.log(uploadFile.getUploads());
                }
            },
            {
                xtype: 'fineUploaderSingle',
                config: {
                    tplId: 'ui2', // 模板id
                    //uploadButtonText: '我要上传。。。',
                    autoUpload: true, // 自动上传
                    showProgress: false, // 不显示上传进度条
                    request: {
                        url: cfg.requestUrl + 'services/fileUpload/multipicupload' + cfg.suffix, // 上传接口
                        params: { name: '' } // 自定义request参数
                    },
                    deleteFileURL: cfg.requestUrl + 'services/fileUpload/deletepicupload' + cfg.suffix, // 删除接口
                    validation: {
                        sizeLimit: 51200000, // 自定义上传文件大小
                        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'zip', 'rar'] // 自定义上传文件类型
                    },
                    onValidate: function (singleUploader, obj, btn) {
                        // 自定义验证hock根据需要自行加入
                        console.log('onValidate//...', arguments);
                        singleUploader.setParams({ name: 'aaabbb' });
                        //return false; // 拒绝上传档案
                    },
                    onUpload: function (singleUploader, id, name) {
                        console.log('onUpload', arguments);
                    },
                    onBeforeUpload: function () {
                        // 上传提交前的hock根据需要自行加入
                        //return false; // 拒绝上传
                    },
                    onProgress: function (id, name, uploadedBytes, totalBytes) {
                        var currentPercent = Math.ceil((uploadedBytes / totalBytes) * 100);
                        Ext.getCmp('uploadProgressText').setText(currentPercent + '%');
                        Ext.getCmp('uploadProgressBar').setStyle('width', currentPercent + '%');
                    },
                    onComplete: function (singleUploader, id, name, responseJSON, maybeXhr) {
                        // 上传完成后的hock根据需要自行加入
                        console.log('onComplete//...', arguments);
                    }
                    /*
                    初始化上传文件
                    initialFiles: {
                        "name": "a3ef2360-881d-452c-a5f6-a173d5291066.jpg",
                        "uuid": "a3ef2360-881d-452c-a5f6-a173d5291066",
                        "size": "66000",
                        "thumbnailUrl": "http://192.168.2.122:8080/ms.common.uploadFile/picupload.jsx?method=preview&path=666/201704/09b8085fc43b4d70a8a3708d6976a255.jpg"
                    }*/
                }
            },
            {
                xtype: 'container',
                layout: {
                    type: 'hbox',
                    align: 'left'
                },
                items: [{
                    xtype: 'label',
                    text: '上传进度',
                    style: {
                        color: 'black'
                    },
                    height: 22,
                    margin: '0 10 0 0'
                }, {
                    xtype: 'container',
                    width: 500,
                    height: 22,
                    style: {
                        borderRadius: '8px',
                        border: '1px solid blue',
                        overflow: 'hidden'
                    },
                    items: [{
                        xtype: 'box',
                        id: 'uploadProgressBar',
                        height: 20,
                        style: {
                            width: '0%',
                            backgroundColor: 'red',
                            borderRadius: '8px'
                        }
                    }]
                }, {
                    xtype: 'label',
                    id: 'uploadProgressText',
                    text: '',
                    style: {
                        color: 'blue'
                    },
                    height: 22,
                    margin: '0 0 0 10'
                }]
            },
            {
                xtype: 'button',
                text: '获取附件列表',
                margin: '20 10',
                handler: function () {
                    var me = this;
                    var uploadFile = me.up().down('fineUploaderSingle');
                    console.log(uploadFile.getUploads());
                }
            },
            {
                xtype: 'button',
                text: '点我上传文件',
                margin: '20 10',
                handler: function () {
                    var win = Ext.create('Ext.window.Window', {
                        title: '上传文件',
                        width: 380,
                        layout: 'fit',
                        padding: 10,
                        items: {
                            xtype: 'panel',
                            scrollable: false,
                            items: [{
                                xtype: 'fineUploaderSingle',
                                config: {
                                    //tplId: 'ui2',
                                    request: {
                                        url: cfg.requestUrl + 'services/fileUpload/multipicupload' + cfg.suffix, // 上传接口
                                        params: { name: '' } // 自定义request参数
                                    },
                                    deleteFileURL: cfg.requestUrl + 'services/fileUpload/deletepicupload' + cfg.suffix, // 删除接口
                                    validation: {
                                        sizeLimit: 512000, // 1kb = 1024 bytes
                                        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'zip']
                                    },
                                    onValidate: function (singleUploader, obj, btn) {
                                        console.log('onValidate//...', arguments);
                                        singleUploader.setParams({ name: 'aaabbb' });
                                        //return false;
                                    },
                                    onUpload: function (singleUploader, id, name) {
                                        console.log('onUpload', arguments);

                                    },
                                    onBeforeUpload: function () {
                                        //return false;
                                    },
                                    onComplete: function (singleUploader, id, name, responseJSON, maybeXhr) {
                                        console.log('onComplete//...', arguments);
                                    }
                                }
                            }, {
                                bbar: [
                                    '->',
                                    '->',
                                    {
                                        xtype: 'button',
                                        text: '保存',
                                        margin: '20 10',
                                        handler: function () {
                                            var me = this;
                                            var uploadFile = me.up().up().up().down('fineUploaderSingle');
                                            console.log(uploadFile.getUploads());
                                            win.close();
                                        }
                                    }
                                ]
                            }]

                        }
                    }).show();
                }
            }
        ];
        me.callParent();
    }
})